package com.pz.cloud.framework.config.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pz.cloud.framework.business.config.entity.BizConfig;
import com.pz.cloud.framework.business.config.service.IBizConfigService;
import com.pz.cloud.framework.config.mapper.BizConfigMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class BizConfigServiceImpl extends ServiceImpl<BizConfigMapper, BizConfig> implements IBizConfigService {

    @Override
    public List<BizConfig> getByModule(String module) {
        LambdaQueryWrapper<BizConfig> query = new LambdaQueryWrapper<BizConfig>()
                .eq(BizConfig::getModule, module);
        return list(query);
    }

    @Override
    public BizConfig getByModuleAndCode(String module, String code) {
        LambdaQueryWrapper<BizConfig> query = new LambdaQueryWrapper<BizConfig>()
                .eq(BizConfig::getModule, module)
                .eq(BizConfig::getCode, code);
        return getOne(query);
    }

    @Override
    public boolean save(BizConfig entity) {
        boolean flag = false;
        if(StringUtils.isEmpty(entity.getId())){
            flag = super.save(entity);
        }else{
            BizConfig exisisEntity = super.getById(entity.getId());
            if (exisisEntity != null) {
                flag = super.updateById(entity);
            }
        }
        return flag;
    }
}
